package com.bnz.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bnz.shop.dao.SpecificationDao;
import com.bnz.shop.dao.SpecificationOptionDao;
import com.bnz.shop.dao.TypeTemplateDao;
import com.bnz.shop.entity.SpecificationEntity;
import com.bnz.shop.entity.SpecificationOptionEntity;
import com.bnz.shop.entity.TypeTemplateEntity;
import com.bnz.shop.group.Specification;
import com.bnz.shop.service.SpecificationService;
import com.bnz.utils.PageUtils;
import com.bnz.utils.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

	@Autowired
	private SpecificationOptionDao optionDao;
	@Resource
	private TypeTemplateDao templateDao;
	/**
	 * 1. 分页带条件查询
	 * @param params
	 * @return
	 */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );
        
        return new PageUtils(page);
    }

	/**
	 * 2. 添加规格的方法
	 * @param specification
	 */
	@Override
	public void save(Specification specification) {
		//2.1 先添加规格选项
		SpecificationEntity spec = specification.getSpec();
		baseMapper.insert(spec);
		System.out.println("spec.getId() = " + spec.getId());
		//2.2 添加完成后就可以得到规格的id，将此id与规格选项绑定
		for (SpecificationOptionEntity option : specification.getOptions()) {
			//2.2.1 设置规格选项的外键
			option.setSpecId(spec.getId());
			//2.2.2 开始添加规格选项
			optionDao.insert(option);
		}

	}

	/**
	 * 3. 根据规格id查询出规格及规格选项列表
	 * @param id
	 * @return
	 */
	@Override
	public Specification findById(String id) {
		//3.1 根据id查询出规格
		SpecificationEntity spec = baseMapper.selectById(id);
		//3.2 根据规格id查询出规格选项列表
		List<SpecificationOptionEntity> optiions = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", spec.getId()));
		//3.3 返回规格及规格选项列表的组合类
		return new Specification(spec,optiions);
	}

	//4. 修改规格及规格选项
	@Override
	public void update(Specification specification) {
		//4.1 先修改规格
		baseMapper.updateById(specification.getSpec());
		//4.2 再根据外键id（规格的主键值）来在规格选项表中删除原来的所有规格列表
		optionDao.delete(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",specification.getSpec().getId()));

		//4.3 再重新添加规格列表
		for (SpecificationOptionEntity option : specification.getOptions()) {
			//4.3.1 绑定与规格的id的外键关系
			option.setSpecId(specification.getSpec().getId());
			//4.3.2 添加规格
			optionDao.insert(option);
		}
	}

	/**
	 * 5. 根据id批量删除
	 * @param ids
	 */
	@Override
	public void deleteAll(List<String> ids) {
		//5.1 删除规格选项
		for (String id : ids) {
			optionDao.delete(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
			//5.2 删除规格
			baseMapper.deleteById(id);
		}

	}

	/**
	 * 6. 根据模板id查询规格及规格选项列表
	 * @param id
	 * @return
	 */
	@Override
	public List<Map> findSpecList(String id) {
		//6.1 查询出模板对象
		TypeTemplateEntity templateEntity = templateDao.selectById(id);
		//6.2 得到规格列表
		String specIds = templateEntity.getSpecIds();
		//6.3 转换为对象
		List<Map> specMap = JSON.parseArray(specIds,Map.class);
		//6.4 遍历上面的集合
		for (Map map : specMap) {
			//6.4.1 得到规格id
			String specId = map.get("id").toString();
			//6.4.2 根据规格id找出规格选项列表
			List<SpecificationOptionEntity> options = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
			//6.4.3 向map中放一个名为options的key
			map.put("options",options);
		}
		//6.5 返回
		return specMap;
	}

}